package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.AnnualLeaveRuleBo;
import org.eiahe.hr.attendance.domain.vo.AnnualLeaveRuleVo;
import org.eiahe.hr.attendance.service.IAnnualLeaveRuleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 年假规则管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/annualLeaveRule")
public class AnnualLeaveRuleController extends BaseController {

    private final IAnnualLeaveRuleService annualLeaveRuleService;

    /**
     * 查询年假规则列表
     */
    @SaCheckPermission("attendance:annualLeaveRule:list")
    @GetMapping("/list")
    public TableDataInfo<AnnualLeaveRuleVo> list(AnnualLeaveRuleBo bo, PageQuery pageQuery) {
        return annualLeaveRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取年假规则详细信息
     */
    @SaCheckPermission("attendance:annualLeaveRule:query")
    @GetMapping("/{ruleId}")
    public R<AnnualLeaveRuleVo> getInfo(@PathVariable Long ruleId) {
        return R.ok(annualLeaveRuleService.queryById(ruleId));
    }

    /**
     * 新增年假规则
     */
    @SaCheckPermission("attendance:annualLeaveRule:add")
    @Log(title = "新增年假规则", linkType = "attendance", businessType = BusinessType.INSERT, nameField = "ruleName")
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AnnualLeaveRuleBo bo) {
        return toAjax(annualLeaveRuleService.insertByBo(bo));
    }

    /**
     * 修改年假规则
     */
    @SaCheckPermission("attendance:annualLeaveRule:edit")
    @Log(title = "修改年假规则", linkType = "attendance", businessType = BusinessType.UPDATE, nameField = "ruleName")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AnnualLeaveRuleBo bo) {
        return toAjax(annualLeaveRuleService.updateByBo(bo));
    }

    /**
     * 删除年假规则
     */
    @SaCheckPermission("attendance:annualLeaveRule:remove")
    @Log(title = "删除年假规则", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@PathVariable Long[] ruleIds) {
        return toAjax(annualLeaveRuleService.deleteWithValidByIds(List.of(ruleIds)));
    }

    /**
     * 激活年假规则
     */
    @SaCheckPermission("attendance:annualLeaveRule:active")
    @Log(title = "激活年假规则", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/active/{ruleId}")
    public R<Void> active(@PathVariable Long ruleId) {
        return toAjax(annualLeaveRuleService.activeRule(ruleId));
    }

    /**
     * 查询当前生效的年假规则
     */
    @SaCheckPermission("attendance:annualLeaveRule:query")
    @GetMapping("/active")
    public R<AnnualLeaveRuleVo> getActiveRule() {
        return R.ok(annualLeaveRuleService.queryActiveRule());
    }

    /**
     * 导出年假规则列表
     */
    @SaCheckPermission("attendance:annualLeaveRule:export")
    @Log(title = "导出年假规则", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AnnualLeaveRuleBo bo, HttpServletResponse response) {
        List<AnnualLeaveRuleVo> list = annualLeaveRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "年假规则数据", AnnualLeaveRuleVo.class, response);
    }

    @GetMapping("/getInfoByDay/{days}")
    public R<AnnualLeaveRuleVo> getInfoByDay(@PathVariable Integer days) {
        return R.ok(annualLeaveRuleService.getInfoByDay(days));
    }
}
